{% import _self as self %}<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * {{ cache_class }}
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class {{ cache_class }} extends {{ base_class }}
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();

{% for webspace in collection.webspaces %}
        // new webspace
        $webspace = new Webspace();
        $webspace->setKey('{{ webspace.key }}');
        $webspace->setName({{ webspace.name|json_encode(constant('JSON_UNESCAPED_UNICODE'))|raw }});
{% if webspace.security != null %}
        $security = new Security();
        $security->setSystem('{{ webspace.security.system }}');
        $security->setPermissionCheck({{ webspace.security.permissionCheck ? 'true' : 'false' }});
        $webspace->setSecurity($security);
{% endif %}
{% for localization in webspace.localizations %}
        // add localization to webspace
        $localization0 = new Localization('{{ localization.language }}', '{{ localization.country }}');
        $localization0->setShadow('{{ (localization.shadow) }}');
        $localization0->setDefault({{ localization.default ? 'true' : 'false' }});
{% for childLocalization in localization.children %}
{{ self.create_localizations(childLocalization, 1, 0, webspace) }}
{% endfor %}
        $localizationRefs['{{ webspace.key }}_{{ localization.localization }}'] = $localization0;
        $webspace->addLocalization($localization0);
{% endfor %}
{% for segment in webspace.segments %}
        // add segment to webspace
        $segment = new Segment();
        $segment->setKey('{{ segment.key }}');
        $segment->setMetadata({{ self.renderArray(segment.metadata)}});
        $segment->setDefault({{ segment.default ? 'true' : 'false' }});
        $webspace->addSegment($segment);
        $segmentRefs['{{ webspace.key }}_{{ segment.key }}'] = $segment;

{% endfor %}
        // add theme
{% if webspace.theme != null %}
        $webspace->setTheme('{{ webspace.theme }}');
{% endif %}


{% for type,template in webspace.templates %}        $webspace->addTemplate('{{ type|raw }}', '{{ template }}');
{% endfor %}
{% for type,template in webspace.defaultTemplates %}        $webspace->addDefaultTemplate('{{ type }}', '{{ template }}');
{% endfor %}
{% for template in webspace.excludedTemplates %}        $webspace->addExcludedTemplate('{{ template }}');
{% endfor %}

        // add navigation
        $navigation = new Navigation();
{% for context in webspace.navigation.contexts %}        $navigation->addContext(new NavigationContext('{{ context.key }}', {{ self.renderArray(context.metadata) }}));
{% endfor %}        $webspace->setNavigation($navigation);
        $webspace->setResourceLocatorStrategy('{{ webspace.resourceLocator.strategy }}');

{% for portal in webspace.portals %}

        // new portal
        $portal = new Portal();
        $portal->setName('{{ portal.name }}');
        $portal->setKey('{{ portal.key }}');
        $portal->setWebspace($webspace);
{% for localization in portal.localizations %}

        // add localization
        $localization = new Localization('{{ localization.language }}', '{{ localization.country }}');
        $localization->setDefault('{{ localization.default }}');
        $portal->addLocalization($localization);
{% endfor %}

{% for environment in portal.environments %}

        // add environment
        $environment = new Environment();
        $environment->setType('{{ environment.type }}');
{% for url in environment.urls %}

        // add environment url
        $url = new Url();
        $url->setUrl('{{ url.url }}');
        $url->setLanguage('{{ url.language }}');
        $url->setCountry('{{ url.country }}');
        $url->setSegment('{{ url.segment }}');
        $url->setRedirect('{{ url.redirect }}');
        $url->setMain({{ url.main?'true':'false' }});
        $environment->addUrl($url);
{% endfor %}
{% for customUrl in environment.customUrls %}
        $environment->addCustomUrl(new CustomUrl('{{ customUrl.url }}'));
{% endfor %}

        $portal->addEnvironment($environment);
{% endfor %}
        $portalRefs['{{ portal.key }}'] = $portal;
        $webspace->addPortal($portal);

{% endfor %}
        $webspaceRefs['{{ webspace.key }}'] = $webspace;


{% endfor %}

{% for environmentKey, environment in collection.portalInformations %}
{% for portalInformation in environment %}
        $portalInformationRefs['{{ environmentKey }}']['{{ portalInformation.url }}'] = new PortalInformation(
            type: {{ portalInformation.type }},
            webspace: $webspaceRefs['{{ portalInformation.webspace }}'],
{% if portalInformation.portal %}
            portal: $portalRefs['{{ portalInformation.portal }}'],
{% endif %}
{% if portalInformation.localization %}
            localization: $localizationRefs['{{ portalInformation.webspace }}_{{ portalInformation.localization.localization }}'],
{% endif %}
            url: '{{ portalInformation.url }}',
{% if portalInformation.redirect %}
            redirect: '{{ portalInformation.redirect }}',
{% endif %}
{% if portalInformation.main %}
            main: true,
{% endif %}
{% if portalInformation.urlExpression %}
            urlExpression: '{{ portalInformation.urlExpression }}',
{% endif %}
            priority: {{ portalInformation.priority }}
        );

{% endfor %}
{% endfor %}

        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}

{% macro renderArray(array) %}{% import _self as self %}
    array(
    {% for key, value in array %}
        {% if value is iterable %}
            '{{ key }}' => {{ self.renderArray(value) }},
        {% else %}
            '{{ key }}' => '{{ value }}',
        {% endif %}
    {% endfor %}
    )
{% endmacro %}

{% macro create_localizations(localization, count, parent, webspace) %}{% import _self as self %}
    // parent id {{ parent }}
    $localization{{ count }} = new Localization('{{ localization.language }}', '{{ localization.country }}');
    $localization{{ count }}->setShadow('{{ (localization.shadow) }}');
    $localization{{ count }}->setParent($localization{{ parent }});
    $localization{{ count }}->setDefault('{{ localization.default }}');

    $localizationRefs['{{ webspace.key }}_{{ localization.localization }}'] = $localization{{ count }};

    {% for childLocalization in localization.children %}
        {{ self.create_localizations(childLocalization, count + 1, count) }}
    {% endfor %}
    $localization{{ count - 1 }}->addChild($localization{{ count }});
{% endmacro %}
